local U = require "packages/utility/utility"
local getAllCardNames = function(card_type, true_name)
    local all_names = {}
    local basics = {}
    local normalTricks = {}
    local delayedTricks = {}
    local equips = {}
    for _, card in ipairs(Fk.cards) do
        if card.package and not table.contains(Fk:currentRoom().disabled_packs, card.package.name) and not card.is_derived then
            if card.type == Card.TypeBasic then
                table.insertIfNeed(basics, true_name and card.trueName or card.name)
            elseif card.type == Card.TypeTrick and card.sub_type ~= Card.SubtypeDelayedTrick then
                table.insertIfNeed(normalTricks, true_name and card.trueName or card.name)
            elseif card.type == Card.TypeTrick and card.sub_type == Card.SubtypeDelayedTrick then
                table.insertIfNeed(delayedTricks, true_name and card.trueName or card.name)
            elseif card.type == Card.TypeEquip then
                table.insertIfNeed(equips, true_name and card.trueName or card.name)
            end
        end
    end
    if card_type:find("b") then
        table.insertTable(all_names, basics)
    end
    if card_type:find("t") then
        table.insertTable(all_names, normalTricks)
    end
    if card_type:find("d") then
        table.insertTable(all_names, delayedTricks)
    end
    if card_type:find("e") then
        table.insertTable(all_names, equips)
    end
    return all_names
end
local getNumberStr = function(num)
    if num == 1 then
        return "A"
    elseif num == 11 then
        return "J"
    elseif num == 12 then
        return "Q"
    elseif num == 13 then
        return "K"
    elseif num == 0 then
        return "X"
    end
    return tostring(num)
end

local xingqi = fk.CreateSkill{
    name = "ym1__xingqi",
    attached_skill_name = "ym1__xingqi_global&",
}

Fk:loadTranslationTable{
    ["ym1__xingqi"] = "星启",
    [":ym1__xingqi"] = "出牌阶段外牌堆顶的牌对你可见。所有角色可以将各自场上一张牌当任意基本牌使用，若与牌堆顶牌颜色不同，你摸一张牌令之无效。",
    ["ym1__xingqi_global&"] = "星启",
    [":ym1__xingqi_global&"] = "你可以将自己场上一张牌当任意基本牌使用。",
    ["@[private]ym1__xingqi"] = "星启",

    ["$ym1__xingqi1"] = "先谋后事者昌，先事后谋者亡！",
    ["$ym1__xingqi2"] = "司马氏虽权尊势重，吾等徐图亦无不可！",
}

xingqi:addEffect("viewas",{
    expand_pile = function (self,player)
        return player:getCardIds("j")
    end,
    pattern = ".|.|.|.|.|basic",
    interaction = function(self,player)
        local all_names = getAllCardNames("b")
        local names = player:getViewAsCardNames(xingqi.name, all_names)
        if #names > 0 then
          return UI.CardNameBox { choices = names, all_choices = all_names }
        end
    end,
    card_filter = function(self, player, to_select, selected)
        return #selected == 0 and table.contains(Self:getCardIds("ej"),to_select)
    end,
    view_as = function (self, player, cards)
        if #cards ~= 1 or not self.interaction.data then return end
        local card = Fk:cloneCard(self.interaction.data)
        card:addSubcard(cards[1])
        card.skillName = xingqi.name
        return card
    end,
    enabled_at_play = function (self, player)
        return #player:getCardIds("ej")>0
    end,
    enabled_at_response = function (self, player)
        return #player:getCardIds("ej")>0
    end,
    before_use = function (self, player, use)
        local room = player.room
        local card = use.card
        if card.color ~= Fk:getCardById(room.draw_pile[1]).color then
            room:moveCardTo(use.card, Card.DiscardPile, nil, fk.ReasonJustMove, xingqi.name, nil, true, player.id)
            player:drawCards(1,xingqi.name)
            return ""
        end
    end,
})
xingqi:addAcquireEffect(function (self, player, is_start)
    local room = player.room
    local draw_pile = room.draw_pile
    if player:hasSkill(xingqi.name) then
        local card = Fk:getCardById(draw_pile[1])
        local ret = Fk:translate(card.name)
        if #ret > 9 then
            ret = string.sub(ret, 1, 6)
        end
        ret = ret .. Fk:translate("log_" .. card:getSuitString())
        if card.color == Card.Red then
            ret = ret .. "<font color=#CC3131>".. getNumberStr(card.number) .."</font>"
        else
            ret = ret .. getNumberStr(card.number)
        end
        U.setPrivateMark(player, "ym1__xingqi", {ret})
    end
end)
xingqi:addLoseEffect(function (self, player, is_start)
    player.room:setPlayerMark(player, "@[private]ym1__xingqi", 0)
end)
xingqi:addEffect(fk.AfterCardsMove,{
    can_refresh = function (self, event, target, player, data)
        return player:hasSkill(xingqi.name) and ((target == player and player.phase == Player.Play) or not table.contains({fk.EventPhaseStart, fk.EventPhaseEnd},event))
    end,
    on_refresh = function(self, event, target, player, data)
        local room = player.room
        local draw_pile = room.draw_pile
        if player:hasSkill(xingqi.name) and player.phase ~= Player.Play then
            local card = Fk:getCardById(draw_pile[1])
            local ret = Fk:translate(card.name)
            if #ret > 9 then
                ret = string.sub(ret, 1, 6)
            end
            ret = ret .. Fk:translate("log_" .. card:getSuitString())
            if card.color == Card.Red then
                ret = ret .. "<font color=#CC3131>".. getNumberStr(card.number) .."</font>"
            else
                ret = ret .. getNumberStr(card.number)
            end
            U.setPrivateMark(player, "ym1__xingqi", {ret})
        end
    end,
})
xingqi:addEffect(fk.AfterDrawPileShuffle,{
    can_refresh = function (self, event, target, player, data)
        return player:hasSkill(xingqi.name) and ((target == player and player.phase == Player.Play) or not table.contains({fk.EventPhaseStart, fk.EventPhaseEnd},event))
    end,
    on_refresh = function(self, event, target, player, data)
        local room = player.room
        local draw_pile = room.draw_pile
        if player:hasSkill(xingqi.name) and player.phase ~= Player.Play then
            local card = Fk:getCardById(draw_pile[1])
            local ret = Fk:translate(card.name)
            if #ret > 9 then
                ret = string.sub(ret, 1, 6)
            end
            ret = ret .. Fk:translate("log_" .. card:getSuitString())
            if card.color == Card.Red then
                ret = ret .. "<font color=#CC3131>".. getNumberStr(card.number) .."</font>"
            else
                ret = ret .. getNumberStr(card.number)
            end
            U.setPrivateMark(player, "ym1__xingqi", {ret})
        end
    end,
})
xingqi:addEffect(fk.EventPhaseEnd,{
    can_refresh = function (self, event, target, player, data)
        return player:hasSkill(xingqi.name) and ((target == player and player.phase == Player.Play) or not table.contains({fk.EventPhaseStart, fk.EventPhaseEnd},event))
    end,
    on_refresh = function(self, event, target, player, data)
        local room = player.room
        local draw_pile = room.draw_pile
        if player:hasSkill(xingqi.name) then
            local card = Fk:getCardById(draw_pile[1])
            local ret = Fk:translate(card.name)
            if #ret > 9 then
                ret = string.sub(ret, 1, 6)
            end
            ret = ret .. Fk:translate("log_" .. card:getSuitString())
            if card.color == Card.Red then
                ret = ret .. "<font color=#CC3131>".. getNumberStr(card.number) .."</font>"
            else
                ret = ret .. getNumberStr(card.number)
            end
            U.setPrivateMark(player, "ym1__xingqi", {ret})
        end
    end,
})
xingqi:addEffect(fk.EventPhaseStart,{
    can_refresh = function (self, event, target, player, data)
        return player:hasSkill(xingqi.name) and ((target == player and player.phase == Player.Play) or not table.contains({fk.EventPhaseStart, fk.EventPhaseEnd},event))
    end,
    on_refresh = function(self, event, target, player, data)
        local room = player.room
        room:setPlayerMark(player, "@[private]ym1__xingqi", 0)
    end,
})

return xingqi